package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    /**
     * 查询所有角色信息
     * @return
     */
    @Override
    public List<Role> getAllRole() {
        return  roleDao.getAllRole();
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     * 添加角色
     * @param role
     * @param permissionIds
     * @param menuIds
     */
    @Override
    public void addRole(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.addRole(role);
        setAssociation(role.getId(),permissionIds);
        setAssociationMore(role.getId(),menuIds);
    }

    /**
     * 查询角色
     * @param id
     * @return
     */
    @Override
    public Role findRoleById(Integer id) {
        return roleDao.findRoleById(id);
    }

    /**
     * 查询角色对应的权限
     * @param id
     * @return
     */
    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    /**
     * 查询角色对应的菜单
     * @param id
     * @return
     */
    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return  roleDao.findMenuIdsByRoleId(id);

    }

    /**
     * 编辑角色
     * @param role
     * @param permissionIds
     * @param menuIds
     */
    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //删除中间表关系
        roleDao.deleteAssoRoleWithPerm(role.getId());
        setAssociation(role.getId(),permissionIds);
        roleDao.deleteAssoRoleWithMenu(role.getId());
        setAssociationMore(role.getId(),menuIds);
        roleDao.edit(role);
    }

    @Override
    public void deleteRole(Integer id) {
        //先删除关联表
//        roleDao.deleteAssoRoleWithPerm(id);
//        roleDao.deleteAssoRoleWithMenu(id);
        roleDao.deleteRole(id);
    }

    /**
     * 根据角色id找到绑定的用户
     * @param id
     * @return
     */
    @Override
    public List<User> findUserByRoleId(Integer id) {
        return roleDao.findUserByRoleId(id);
    }

    /**
     * 建立多表之间的联系
     * @param roleId
     * @param ids
     */
    public void setAssociation(Integer roleId,Integer[] ids){
        if (ids!=null && ids.length>0){
            for (Integer id : ids) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("id", id);
                roleDao.setAssociation(map);
            }
        }
    }

    public void setAssociationMore(Integer roleId,Integer[] ids){
        if (ids!=null && ids.length>0){
            for (Integer id : ids) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("id", id);
                roleDao.setAssociationMore(map);
            }
        }
    }
}
